/**
 * 
 */
package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Role;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Daniel Cobo
 * <Role>'s DAO.
 */
public class RoleDAO implements GenericInterfaceDAO {

	@Autowired
	 private SessionFactory sessionFactory;
	
	/**
	 * Get list's <Role>
	 */
	@Override
	public List<Role> selectAll() {

	    List<Role> roles = getSession().createQuery("from role").list();
	    return roles;
	}

	/**
	 * Get an <Role>
	 */
	@Override
	public Role selectById(int id) {
		Role role = (Role) getSession().get(Role.class, id);
	    return role;
	}

	/**
	 * delete an <Role>
	 */
	@Override
	public void delete(int id) {
		Role role = selectById(id);
		getSession().delete(role);
		
	}

	/**
	 * Insert an <Role>
	 */
	@Override
	public void insert(Object newRole) {
		 Integer id = (Integer) getSession().save(newRole);
		    ((Role) newRole).setId(id);  
		
	}

	/**
	 * update an <Role>
	 */
	@Override
	public void update(Object role) {
		getSession().merge(role);
		
	}

	/**
	 * Get a session.
	 */
	@Override
	public Session getSession() {
		 return sessionFactory.getCurrentSession();
	}

	

}
